//common footer script
(function(g){
    g.redirect= function(url){
        location.href = url;
    };

    //sidebar nav add active style
    (function(){
        var accept_module=[
            'brand',
            'shop',
            'product',
            'order',
            'user',
            ['privilege','adminuser'],
            'setting'
        ];

        var hf=location.href,
            current_module=hf.match(/admin\/(\w+)\/?([^\?]*)/);

        if(current_module && current_module[1]){
            var m= '/'+ wxi.string.trim(current_module[0], '/');
            var module_name=current_module[1];
            var sidenav= $('.sidebar-menu > li');

            wxi.map(sidenav, function(item, idx){
               var menu_link=$(item).find('.treeview-menu>li>a');

                $(menu_link).each(function(dx){

                    if($(this).attr('href') == m){
                        $(this).parent().addClass('active');
                        $(this).parents('.treeview').addClass('active');
                    }

                });

            });

            wxi.map(accept_module, function(module,idx){
                if(typeof module == 'string' && module == module_name){
                    $(sidenav).eq(idx).addClass('active');
                }
                else if(wxi.isArray(module) && module.indexOf(module_name) !== -1){
                    $(sidenav).eq(idx).addClass('active');
                }
            });

        }

    })();

    //shop admin bundle, common delete ajax logic
    (function(){
        var btn_del=$('.op_icon .btn_del');

        btn_del.on('click', function(){
            var eid=$(this).data('eid'),
                row=$(this).parents('tr'),
                bundle=$(this).parents('ul.op_icon').data('bundle');

            wxi.ui.modal({
                title: '删除对话框',
                msg: '确认删除吗?',
                height: '80px',
                //width:'100%',
                cb: function(){
                    if(bundle==''){
                        wxi.util.tipMsg('','参数错误：缺少bundle');
                        return;
                    }

                    $.get('/admin/'+bundle+ '/'+eid+'/delete', function(d){
                        if(d.success){
                            wxi.util.tipMsg('', '删除成功','',function(){
                                wxi.ui.hideModal();
                                row.remove();
                            });
                        }
                        else{
                            wxi.util.tipMsg('', d.info, '');
                        }

                    }, 'json');
                }
            });

        });
    })();

    //bs page tooltip
    $('[data-toggle="tooltip"]').tooltip({
        placement: 'bottom',
        trigger: 'hover'
    });

    $("select").select2();

    $('.datepicker').datepicker({
        format: 'yyyy/mm/dd',
        language: 'zh-CN',
        orientation:'bottom'
    });

    $('.daterange').daterangepicker({
        locale: {
            applyLabel: '确定',
            cancelLabel: '取消',
            fromLabel: '从',
            toLabel: '到',
            weekLabel: 'W',
            customRangeLabel: '自定义',
            //daysOfWeek: moment.weekdaysMin(),
            //monthNames: moment.monthsShort(),
            firstDay: moment.localeData()._week.dow,

            /* 区域化周名为中文 */
            daysOfWeek : ["日", "一", "二", "三", "四", "五", "六"],
            /* 每周从周一开始 */
            //firstDay : 1,
            /* 区域化月名为中文习惯 */
            monthNames : ["1月", "2月", "3月", "4月", "5月", "6月",
                "7月", "8月", "9月", "10月", "11月", "12月"]
        },

        format: 'YYYY/MM/DD'
    });

    $('#btn-logout').on('click', function(){
        $.get('/admin/logout', function(d){
            wxi.util.tipMsg('', '退出系统...',800, function(){
                redirect('/admin/login');
            });
        });
    });





})(this);